//
//  HZOtherView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/20.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SnapKit

class HZOtherView: UIView {

    //定义约束对象
    var constra:Constraint?
    
    //一有数据就赋值文本
    var cellVM:HZHomeCellVm? {
        
        didSet{
            
            textLabel.attributedText = cellVM?.otherAttributedStrinng
            
            
            
            
            //更新配图区域的大小
            updateImg()
        }
        
    }
    
    //更新配图区域的大小
    private func updateImg(){
        
        //先清除约束
        constra?.uninstall()
        
        
        //如果有配图
        if let pic_urls = cellVM?.cellModel?.retweeted_status?.pic_urls where  pic_urls.count > 0{
            
            //显示出来
            iconImg.hidden = false
            
            
            var size:CGSize
            
            
            //判断是单图还是多图
            if cellVM?.cellModel?.retweeted_status?.pic_urls?.count == 1{
                
                size = cellVM?.oneOtherImgSize ?? CGSizeZero
            }else{
                size = cellVM?.otherSize ?? CGSizeZero
            }
            
            //赋值
            iconImg.dataInfo = (pic_urls,size)
            
            
            self.snp_updateConstraints { (make) -> Void in
                constra = make.bottom.equalTo(iconImg.snp_bottom).offset(HZSpacing).constraint
            }

            
            
        } else{
            
            
            //显示隐藏
            iconImg.hidden =  true
            
            self.snp_updateConstraints { (make) -> Void in
                constra = make.bottom.equalTo(textLabel.snp_bottom).offset(HZSpacing).constraint
            }

            
        }
    }
   
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        setupUI()
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: -  添加子控件
    private func setupUI(){
        
        //设置背景色
        backgroundColor = UIColor(white: 230/255.0, alpha: 1)
        
        //添加子控件
        addSubview(textLabel)
        addSubview(iconImg)
        
        
        
        //约束
        textLabel.snp_makeConstraints { (make) -> Void in
            
            
            make.leading.top.equalTo(HZSpacing)
            make.trailing.equalTo(-HZSpacing)
            
        }
        
        //约束配图
        iconImg.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.leading.equalTo(HZSpacing)
            
            //定死了高
            constra = make.height.width.equalTo(50).constraint
        }
        
        self.snp_makeConstraints { (make) -> Void in
            constra = make.bottom.equalTo(iconImg.snp_bottom).offset(HZSpacing).constraint
        }

        
    }
    
    //懒加载子控件
    
    lazy var textLabel:YYLabel = {
        
        let textLabel = YYLabel()
        textLabel.textColor = UIColor.darkGrayColor()
        textLabel.font = UIFont.systemFontOfSize(HZNameTextFont)
        textLabel.preferredMaxLayoutWidth = screenWidth-2*HZSpacing
        textLabel.numberOfLines = 0
        
        return textLabel
    }()

    
    //懒加载配图区域
    lazy var iconImg:HZImageCollView = {
        
        let iconIme = HZImageCollView.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        return iconIme
    }()
    

}
